package com.dormitory.dao.impl;

import com.dormitory.dao.BuildingDao;
import com.dormitory.model.Building;
import com.dormitory.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BuildingDaoImpl implements BuildingDao {
    
    @Override
    public boolean save(Building building) {
        String sql = "INSERT INTO buildings (building_name, total_floors, building_type) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, building.getBuildingName());
            stmt.setInt(2, building.getTotalFloors());
            stmt.setString(3, building.getBuildingType());
            
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Building building) {
        String sql = "UPDATE buildings SET building_name=?, total_floors=?, building_type=? WHERE building_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, building.getBuildingName());
            stmt.setInt(2, building.getTotalFloors());
            stmt.setString(3, building.getBuildingType());
            stmt.setInt(4, building.getBuildingId());
            
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer buildingId) {
        String sql = "DELETE FROM buildings WHERE building_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, buildingId);
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Building findById(Integer buildingId) {
        String sql = "SELECT * FROM buildings WHERE building_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, buildingId);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                return extractBuildingFromResultSet(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Building> findAll() {
        List<Building> buildings = new ArrayList<>();
        String sql = "SELECT * FROM buildings";
        
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                buildings.add(extractBuildingFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buildings;
    }

    @Override
    public List<Building> findByCondition(String condition) {
        List<Building> buildings = new ArrayList<>();
        String sql = "SELECT * FROM buildings WHERE building_name LIKE ? OR building_type LIKE ?";
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, "%" + condition + "%");
            stmt.setString(2, "%" + condition + "%");
            
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                buildings.add(extractBuildingFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buildings;
    }

    private Building extractBuildingFromResultSet(ResultSet rs) throws SQLException {
        Building building = new Building();
        building.setBuildingId(rs.getInt("building_id"));
        building.setBuildingName(rs.getString("building_name"));
        building.setTotalFloors(rs.getInt("total_floors"));
        building.setBuildingType(rs.getString("building_type"));
        return building;
    }
} 